<template>
	<view>
		<view class="">
			<uni-segmented-control :current="current" :values="items" :style-type="styleType"
				:active-color="activeColor" @clickItem="onClickItem" />
		</view>
		<data-list ref="list" :enableBackToTop="true" @emptyTo="toIndex()" @load="page" @refresh="pageRefresh">
			<cf-goods :list="goods" @click="goodsDetail" @addCard="addCard"></cf-goods>
		</data-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				items: ['综合', '销量', '价格'],
				current: 0,
				activeColor: '#007aff',
				styleType: 'text',
				goods: [],
				title: '',
				classId: '',
				order: 1,
			}
		},
		onLoad(options) {
			console.log(options.title)
			this.title = options.title || '';
			this.classId = options.classId || '';
			// this.getGoods(1)
		},
		methods: {
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
					this.query(e.currentIndex)
				}
			},
			query(index) {
				if (index == 0) {
					//综合-时间倒叙
					this.order = 1
				} else if (index == 1) {
					//销量
					this.order = 2
				} else if (index == 2) {
					//价格
					this.order = 3
				}
				this.$refs.list.refreshData()
			},
			pageRefresh(data, callback) {
				this.goods = [];
				this.page(data, callback)
			},
			page(data, callback) {
				this.apis.goodsList({
					page: data.page,
					limit: data.size,
					type: 0,
					order: this.order,
					classId: this.classId,
					name: this.title
				}).then(res => {
					this.goods = [...this.goods, ...res.records || []];
					callback({
						list: this.goods,
						total: res.total || 0
					});
				})
			},
			//商品详情
			goodsDetail(id) {

				uni.navigateTo({
					url: '/pagesOne/goodsDetail/goodsDetail?id=' + id
				});
			},
			//加入购物车
			addCard(id) {
				this.apis.addCar({
					goodsId: id,
					number: 1
				}).then(res => {
					uni.showToast({
						title: "加入成功！"
					})
					// uni.setTabBarBadge({
					// 	index: 2,
					// 	text: res + ""
					// })
					// this.getCarCount()
				})
			},
			toIndex() {
				uni.switchTab({
					url: "/pages/index/index"
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
